package ym.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import ym.mapper.ProductRecordMapper;
import ym.pojo.ProductRecord;

import java.util.List;

@DubboService
@Transactional
public class ProductRecordServiceImpl implements ProductRecordService {
    @Autowired
    private ProductRecordMapper productRecordMapper;

    @Override
    public void add(ProductRecord productRecord) {
        productRecordMapper.insert(productRecord);
    }

    @Override
    public void update(ProductRecord productRecord) {
        productRecordMapper.updateById(productRecord);
    }

    @Override
    public void delete(Integer id) {
        productRecordMapper.deleteById(id);
    }

    @Override
    public ProductRecord findById(Integer id) {
        return productRecordMapper.selectById(id);
    }

    @Override
    public List<ProductRecord> findAll() {
        return productRecordMapper.selectList(null);
    }

    @Override
    public Page<ProductRecord> search(String search, int page, int size) {
        QueryWrapper<ProductRecord> queryWrapper = new QueryWrapper<>();
        if (search != null) {

        }
        return productRecordMapper.selectPage(new Page(page, size), queryWrapper);
    }

    @Override
    public Page<ProductRecord> searchByNutritionProductId(Integer nutritionProductId, String search, int page, int size) {
        QueryWrapper<ProductRecord> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.eq("nutritionProductId", nutritionProductId);
        } else {
            queryWrapper.eq("nutritionProductId", nutritionProductId);
        }
        return productRecordMapper.selectPage(new Page(page, size), queryWrapper);
    }
}
